package com.example.endwork;

import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.core.view.WindowInsetsCompat;

import com.dao.DBHelp;
import com.dao.DBoperate;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.items.MyAdapter;
import com.items.person;
import com.operate.addData;

import java.util.ArrayList;
import java.util.List;

public class homePage extends AppCompatActivity {
    private ListView list;
    private DBoperate dboperate;
    private List<person> data;//放在列表中的数据
    private FloatingActionButton insert;
    private FloatingActionButton search;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_home_page);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // 数据列表项
        this.List_init();
        // 添加数据按钮
        this.insert=findViewById(R.id.fab);
        this.insertListener();
        //元素监听：点击元素
        this.ListElementListener();
        this.searchListener();

    }

    private void searchListener(){
        search=findViewById(R.id.search);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(homePage.this, find.class);
                startActivity(intent);
            }
        });
    }

    public void insertListener(){
        this.insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(homePage.this, page2.class);
                intent.putExtra("type", "E");
                startActivity(intent);
            }
        });
    }

    public void List_init(){
        list=findViewById(R.id.listView);
        dboperate=new DBoperate((new DBHelp(this)));
        data=dboperate.getAll();
        MyAdapter adapter=new MyAdapter(this,data);
        list.setAdapter(adapter);

    }

    private void ListElementListener(){
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //跳转到详情界面
                Intent intent = new Intent(homePage.this, page2.class);
                person value=data.get(i);
                intent.putExtra("type", "A");
                intent.putExtra("name", value.getName());
                intent.putExtra("p1", value.getPhone_1());
                intent.putExtra("p2", value.getPhone_2());
                homePage.this.startActivity(intent);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        //刷新数据
        List_init();
    }
}